package cn.dapeng.hash;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.UUID;

/**
 * 这个程序有问题 主要是因为hashMap没有锁导致的
 */
public class HashMap_T02 {

    private final static HashMap<UUID, UUID> m = new HashMap<>();

    private static final UUID[] key = new UUID[Constant.COUNT];

    private static final UUID[] value = new UUID[Constant.COUNT];

    static {
        for (int i = 0; i < Constant.COUNT; i++) {
            key[i] = UUID.randomUUID();
            value[i] = UUID.randomUUID();
        }
    }

    static class MyThread extends Thread {

        private final int start;
        private final int gap;

        public MyThread(int start, int gap) {
            this.start = start;
            this.gap = gap;
        }

        @Override
        public void run() {
            for (int i = start; i < start + gap; i++) {
                m.put(key[i], value[i]);
            }
        }
    }


    public static void main(String[] args) throws Exception {

        long start = System.currentTimeMillis();
        int gap = Constant.COUNT / Constant.THREAD_COUNT;

        Thread[] threads = new Thread[Constant.COUNT];
        for (int i = 0; i < Constant.THREAD_COUNT; i++) {
            threads[i] = new MyThread(i * gap, gap);
        }

        for (int i = 0; i < Constant.THREAD_COUNT; i++) {
            threads[i].start();
        }

        for (int i = 0; i < Constant.THREAD_COUNT; i++) {
            threads[i].join();
        }
        System.out.println("size = " + m.size());
        long end = System.currentTimeMillis();
        System.out.println("execute time-length:" + (end - start));
    }


}
